package fei.yuzhi.com.loveflowers.fragment.firstpagefragment.detail.baseinformation;

import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fei.yuzhi.com.loveflowers.bean.firstpagedetailbean.FirstPageBaseDetailBean;
import fei.yuzhi.com.loveflowers.bean.firstpagedetailbean.GoodsCommendListBean;
import fei.yuzhi.com.loveflowers.configer.UrlConfiger;
import fei.yuzhi.com.loveflowers.http.RetrofitService;
import fei.yuzhi.com.loveflowers.utils.FirstPageHttpUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by My on 2016/10/8.
 */
public class BaseInfomationModel implements BaseInfomationContract.IBaseInformationModel {
    private String goodsImageUrl;
    private Map<String,String> mTextViewMap=new HashMap<>();
    private List<GoodsCommendListBean> commendListBeen=new ArrayList<>();

    @Override
    public void getBeanFormNet(final String data, final OnGetBaseInformationListener getBaseInformationListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FirstPageHttpUtils.getBaseDetailBean(data, new FirstPageHttpUtils.OnGetBaseDetailBean() {
                    @Override
                    public void getDataDetailBeanFromNet(FirstPageBaseDetailBean baseDetailBean) {
                        //得到图片的商品地址
                        goodsImageUrl=baseDetailBean.getDatas().getGoods_image();
                        //得到商品详情页面girdview需要放到适配器中的数据集合
                        commendListBeen=baseDetailBean.getDatas().getGoods_commend_list();
                        //得到店铺的名称
                        mTextViewMap.put("Store_name",baseDetailBean.getDatas().getStore_info().getStore_name());
                        //得到商品的名称
                        mTextViewMap.put("Goods_name",baseDetailBean.getDatas().getGoods_info().getGoods_name());
                        if (baseDetailBean.getDatas().getGoods_info().getXianshi_info()!=null){//判断是否为限时销售的商品
                            //如果是，则获取售价以及原售价
                            mTextViewMap.put("goods_price",baseDetailBean.getDatas().getGoods_info().getXianshi_info().getGoods_price());
                            mTextViewMap.put("goods_promotion_price",baseDetailBean.getDatas().getGoods_info().getXianshi_info().getXianshi_price());
                        }else {
                            //如果不是则获取当前的售价
                            mTextViewMap.put("goods_promotion_price",baseDetailBean.getDatas().getGoods_info().getGoods_promotion_price());
                        }
                        //得到商品的市场价
                        mTextViewMap.put("Goods_marketprice",baseDetailBean.getDatas().getGoods_info().getGoods_marketprice());
                        if (goodsImageUrl.indexOf(",")>-1){
                            //判断是否同一个地址在一个字符串内显示多次，如果是则以逗号为分界截取一个完整的图片地址即可
                            goodsImageUrl=goodsImageUrl.substring(0,goodsImageUrl.indexOf(","));
                        }
                        //将物品的库存添加到集合当中
                        mTextViewMap.put("goods_storage",baseDetailBean.getDatas().getGoods_info().getGoods_storage());
                        //将总销量添加到集合当中
                        mTextViewMap.put("goods_salenum",baseDetailBean.getDatas().getGoods_info().getGoods_salenum());
                        //将商品的链接地址放到集合中
                        mTextViewMap.put("goods_url",baseDetailBean.getDatas().getGoods_info().getGoods_url());
                        //将商品的id存放到集合中
                        mTextViewMap.put("data",data);
                        //将所有得到的数据通过model自身的接口回调传递到fragment中
                        getBaseInformationListener.getDetailInformation(goodsImageUrl,mTextViewMap,commendListBeen);
                    }
                });
            }
        }).start();
    }

    public interface OnGetBaseInformationListener{
        void getDetailInformation(String goodImageUrl, Map<String,String> mTextViewMap,
                                  List<GoodsCommendListBean> commendListBeen);
    }
}
